export default {
  data() {
    return {
      upfloclick: true, //true展开收起false
      checkClick: false, //选中
      schemeTyps: '', //
      schemeTypsId: 0, //
      schemeTypexz: false, //方案类型
      numberid: 0, //首付比例
      moreng: false, //设置默认
      morengpage: false, //设置默认

      disabled: false,
      btnWidth: 180,
      show: false,
      options: [{
        text: '删除',
        style: {
          backgroundColor: '#EB5C02',
        },
      }, ],
      PlanList: [{
        proportion: 0,
        show: false, //滑块删除显示
        iconShow: false, //icon显示隐藏
        loanPlan_PlanList: [{
          loanPeriods: 12,
          proportion: 0,
        },
        {
          loanPeriods: 24,
          proportion: 0,
        },
        {
          loanPeriods: 36,
          proportion: 0,
        },
        {
          loanPeriods: 48,
          proportion: 0,
        },
        {
          loanPeriods: 60,
          proportion: 0,
        },
        ],
      }, ], //页面渲染选择使用------------------------------------------------商品规格sku列表

      zancunllist: [], //上个页面传入的所有参数，id等需要用
      PlanList: [], //页面渲染
      submittedvalue: [], //最后提交的值
      moneynum: 0, //裸车价
      MonthlyS: '',
      newsshow: false, //数字键盘
      value: '',
      index: null,
      index1: null,
      indexname: '',
      itemone: '',
      itemtwo: '',
    }
  },

  onLoad(editlist) {
    this.zancunllist = JSON.parse(decodeURIComponent(editlist.datalist))
    this.PlanList = this.zancunllist.planList //页面渲染
    this.PlanList.forEach((item, i) => {
      //提交的值
      this.submittedvalue = this.submittedvalue.concat(item.loanPlan_PlanList)
    })
    if (this.zancunllist.loanPlan_Type === 0) {
      this.schemeTyps = '低首付'
    } else {
      this.schemeTyps = '以租代售'
    }
    this.moneynum = Number(editlist.moneynum)
  },
  methods: {
    keyclick(item, item1, index, index1, name) {
      this.newsshow = true
      this.index = index
      this.index1 = index1
      this.indexname = name
      this.itemone = item
      this.itemtwo = item1
    },
    // 按键被点击(点击退格键不会触发此事件)
    valChange(val) {
      // 将每次按键的值拼接到value变量中，注意+=写法
      var lengthshow = false

      if (this.indexname === 'interestRate') {
        if (this.PlanList[this.index].loanPlan_PlanList[this.index1][this.indexname]) {
          if (this.PlanList[this.index].loanPlan_PlanList[this.index1][this.indexname].length < 3) {
            lengthshow = true
          }
        } else {
          lengthshow = true
        }
      } else {
        // 手续费
        if (this.PlanList[this.index].loanPlan_PlanList[this.index1][this.indexname]) {
          if (this.PlanList[this.index].loanPlan_PlanList[this.index1][this.indexname].length < 7) {
            lengthshow = true
          }
        } else {
          lengthshow = true
        }
      }
      if (lengthshow) {
        this.PlanList[this.index].loanPlan_PlanList[this.index1][this.indexname] = this.PlanList[this.index].loanPlan_PlanList[
          this.index1][this.indexname] + val + ''

        if (this.indexname === 'interestRate') {
          this.GetCarCalculator(
            this.moneynum,
            this.itemone,
            Number(this.PlanList[this.index].loanPlan_PlanList[this.index1][this.indexname]),
            this.itemtwo
          )
        }
      }
    },
    // 退格键被点击
    backspace() {
      // 删除value的最后一个字符
      // this.pageskulist[this.index].children[this.index1][this.indexname] = this.pageskulist[this.index].children[this
      //   .index1][this.indexname].substr(0, this.pageskulist[this.index].children[this.index1][this.indexname].length -
      //   1)
      var listonest = String(this.PlanList[this.index].loanPlan_PlanList[this.index1][this.indexname])
      listonest = listonest.substr(0, listonest.length - 1)
      this.PlanList[this.index].loanPlan_PlanList[this.index1][this.indexname] = Number(listonest)
      if (this.indexname === 'interestRate') {
        this.GetCarCalculator(
          this.moneynum,
          this.itemone,
          Number(this.PlanList[this.index].loanPlan_PlanList[this.index1][this.indexname]),
          this.itemtwo
        )
      }

      console.log(this.value)
    },
    //选择配置，输入
    InputChildItem: function(e, item, item2, htype, index, index1) {
      // console.log(1, e) //输入值
      // console.log(2, item) //渲染表格的值
      // console.log(3, item2) //当前值
      // console.log(4, htype) //当前input

      // 纯数字正则
      var reg = /^[0-9]*$/
      if (!reg.test(e.detail.value)) {
        this.$refs.uToast.show({
          title: '请输入整数',
          type: 'error',
          url: '/pages/user/index',
        })
        this.PlanList[index].loanPlan_PlanList[index1].interestRate = null
        return false
      }
      var olditem = this.submittedvalue.filter(ci => {
        //临时id = 渲染表格的id && 临时名称 = 当前名称   返回临时数据
        if (
          ci.proportion === item.proportion &&
          ci.LoanPeriods === item2.loanPeriods
        ) {
          console.log(1122, ci)
          return ci
        }
      })
      var owneritem = olditem.length > 0 ? olditem[0] : {}
      switch (htype) {
      case 'Guideprice':
        owneritem.interestRate = Number(e.detail.value)
        this.GetCarCalculator(
          this.moneynum,
          item,
          owneritem.interestRate,
          item2
        )
        break
      case 'Quote':
        owneritem.poundage = Number(e.detail.value)
        break
      }

      if (olditem.length > 0) {
        owneritem.carnameid = item.proportion
      } else {
        owneritem.carnameid = item.proportion
        owneritem.LoanPeriods = item2.loanPeriods
        owneritem.proportion = item.proportion
        this.submittedvalue.push(owneritem)
        console.log('this.submittedvalue', this.submittedvalue)
      }

      this.submittedvalue.forEach((value, i) => {
        if (value.proportion === item2.proportion) {
          if (value.loanPeriods === item2.loanPeriods) {
            if (htype === 'Guideprice') {
              value.interestRate = Number(e.detail.value)
            } else {
              value.poundage = Number(e.detail.v0alue)
            }
          }
        }
      })

      // if (olditem.length > 0) {
      //   owneritem.proportion = item.proportion
      // } else {
      //   owneritem.proportion = item.proportion
      //   owneritem.LoanPeriods = item2.loanPeriods
      //   // this.submittedvalue.push(owneritem)
      //   // console.log('this.submittedvalue2', this.submittedvalue)
      //   // this.PlanList.forEach((item6, i) => {
      //   //   if (i === item.proportion) {
      //   //     item.loanPlan_PlanList.forEach((item1, j) => {
      //   //     })
      //   //   }
      //   // })
      // }
    },

    // 返回
    fanhui() {
      //图片地址
      uni.$emit('carImageS', {
        carImageS: this.carImageS,
      })
      uni.navigateBack()
      uni.$off('currentImgs')
    },
    //默认
    lowDownPChange: function(e) {
      if (e) {
        this.moreng = 0
      } else {
        this.moreng = 1
      }
    },
    //点击展开收起
    upClick() {
      this.upfloclick = !this.upfloclick
    },
    // 新增金融
    newFinanSClick() {},
    //选择低首付以租代售
    sTypeClick(type) {
      this.schemeTypexz = false

      if (type === 0) {
        this.schemeTyps = '低首付'
      } else {
        this.schemeTyps = '以租代售'
      }
      this.schemeTypsId = type
    },

    //删除列表
    clickdelect(item, index) {
      this.PlanList[index].loanPlan_PlanList = []
      this.PlanList[index].iconShow = true
      let listbox = [] //暂存赋值
      this.submittedvalue.forEach((item1, i) => {
        if (item.proportion === item1.proportion) {
          item1.proportion = 10
        }
        if (item1.proportion !== 10) {
          listbox.push(item1)
        }
      })
      this.submittedvalue = listbox
      this.$u.toast(`删除了第${index}个列表`)
    },
    // 如果打开一个的时候，不需要关闭其他，则无需实现本方法
    open(index) {
      // 先将正在被操作的swipeAction标记为打开状态，否则由于props的特性限制，
      // 原本为'false'，再次设置为'false'会无效
      this.PlanList[index].show = true
      this.PlanList.map((val, idx) => {
        if (index !== idx) this.PlanList[idx].show = false
      })
      // console.log(this.PlanList)
    },
    //删除之后出现的icon,点击传入id
    iconxzengD(type) {
      this.PlanList.forEach((item, i) => {
        if (item.proportion === type) {
          item.iconShow = false
          item.loanPlan_PlanList = [{
            proportion: item.proportion,
            loanPeriods: 12,
            interestRate: null,
            poundage: 0,
          },
          {
            proportion: item.proportion,
            loanPeriods: 24,
            interestRate: null,
            poundage: 0,
          },
          {
            proportion: item.proportion,
            loanPeriods: 36,
            interestRate: null,
            poundage: 0,
          },
          {
            proportion: item.proportion,
            loanPeriods: 48,
            interestRate: null,
            poundage: 0,
          },
          {
            proportion: item.proportion,
            loanPeriods: 60,
            interestRate: null,
            poundage: 0,
          },
          ]
        }
      })
      // console.log('增加', this.PlanList)
    },
    //点击icon新增
    iconxzeng(type) {
      if (this.PlanList.length > 7) {
        this.$refs.uToast.show({
          title: '只能创建8组',
          type: 'warning',
          url: '/pages/user/index',
        })
        return
      }
      var palid
      this.PlanList.forEach(item => {
        palid = item.proportion
        palid = ++palid
      })
      // console.log('palid', palid)
      // this.numberid++
      let datalist = {
        proportion: palid,
        show: false,
        iconShow: false, //icon显示隐藏
        loanPlan_PlanList: [{
          proportion: palid,
          loanPeriods: 12,
          interestRate: null,
          poundage: 0,
        },
        {
          proportion: palid,
          loanPeriods: 24,
          interestRate: null,
          poundage: 0,
        },
        {
          proportion: palid,
          loanPeriods: 36,
          interestRate: null,
          poundage: 0,
        },
        {
          proportion: palid,
          loanPeriods: 48,
          interestRate: null,
          poundage: 0,
        },
        {
          proportion: palid,
          loanPeriods: 60,
          interestRate: null,
          poundage: 0,
        },
        ],
      }
      this.PlanList = this.PlanList.concat(datalist)
      // this.submittedvalue = this.submittedvalue.concat(datalist.loanPlan_PlanList)
    },
    //编辑
    baocunClick() {
      this.UpdateLoanOlan()
    },
    deleteClick() {
      this.DeleteLoanOlan()
    },
    //编辑金融方案
    async UpdateLoanOlan() {
      let newlist = []
      //把多个数组合并成一个，传给后台的格式就是一个数组。
      this.PlanList.forEach(item => {
        item.loanPlan_PlanList.forEach(item2 => {
          if (item2.interestRate !== null) {
            if (item2.interestRate) {
              item2.interestRate = Number(item2.interestRate)
              if (item2.poundage === null) {
                item2.poundage = 0
              }
              newlist.push(item2)
              // console.log('newkistttttttt', newlist)
            }
          }
        })
      })
      this.submittedvalue = newlist

      if (this.schemeTyps === '低首付') {
        this.schemeTypsId = 0
      } else {
        this.schemeTypsId = 1
      }
      try {
        let data = {
          Id: this.zancunllist.id, //方案id
          Name: this.zancunllist.name, //金融方案名称
          LoanPlan_type: this.schemeTypsId, //数字类型  分类（0低首付，1以租代售）
          Is_Default: this.moreng ? 0 : 1, //默认方案 默认标识(0关闭，1打开)（不同类型只有一个默认）
          PlanList: this.submittedvalue,
          //   'Proportion': 0, //首付比例
          //   'LoanPeriods': 0, //贷款期数
          //   'InterestRate': 0, //贷款利率(如果不填则是-1)
          //   'Poundage': 0 //金融手续费(如果不填则是-1)
          Store_Id: this.$store.state.userInfoDetail.store.storeId, //店铺id
        }
        const res = await this.$api.carSource.UpdateLoanOlan(data)
        if (res.code === 200) {
          setTimeout(function() {
            uni.showToast({
              title: '新增成功',
              duration: 2000,
              icon: 'none',
            })
          }, 100)
          uni.navigateBack()
        }
      } catch (err) {
        console.log(err)
      }
    },
    //删除金融方案
    async DeleteLoanOlan() {
      try {
        let data = {
          Id: this.zancunllist.id,
        }
        const res = await this.$api.carSource.DeleteLoanOlan(data)
        if (res.code === 200) {
          setTimeout(function() {
            uni.showToast({
              title: '删除成功',
              duration: 2000,
              icon: 'none',
            })
          }, 100)
          uni.navigateBack()
        }
      } catch (err) {
        console.log(err)
      }
    },
    //展开
    zhankaiclick(index, item) {
      let listbox = [] //暂存赋值
      let ceshilist = []
      //定义基础数据
      let typelist = [12, 24, 36, 48, 60]
      //判断已有的数据，返回没有的数据
      let temps = typelist.filter(a => {
        if (!item.loanPlan_PlanList.find(lap => lap.loanPeriods === a)) {
          return item
        }
      })
      //给没有的数据进行组装成需要的数组
      let arr = temps.map((num, i) => ({
        proportion: index,
        loanPeriods: num,
        interestRate: null,
        poundage: null,
      }))
      //合并已有的数组和没有的数组
      let sortArr = [...item.loanPlan_PlanList, ...arr]
      //冒泡排序
      sortArr.sort((p, n) => {
        return p.loanPeriods - n.loanPeriods
      })
      //最后赋值
      this.PlanList[index].loanPlan_PlanList = sortArr
    },

    //计算月供
    async GetCarCalculator(Price, downps, Coeff, dqitem) {
      //渲染表格的值  item2当前值
      try {
        let data = {
          salePrice: Price, //销售价格
          payment: downps.proportion, //首付几成
          Coefficient: Coeff, //万元系数
        }
        const res = await this.$api.carSource.GetCarCalculator(data)
        this.MonthlyS = res.data
        this.PlanList.forEach((item, i) => {
          if (i === downps.proportion) {
            item.loanPlan_PlanList.forEach((item1, j) => {
              if (item1.interestRate === dqitem.interestRate) {
                item1.monthly = this.MonthlyS
                console.log('this.MonthlyS1111111111111', this.MonthlyS)
              }
            })
          }
        })
        // console.log('PlanList', this.PlanList)
      } catch (err) {
        console.log(err)
      }
    },
  },
}
